/*
 * CompositeGraphicsObject.java
 *
 * Created on 28. maaliskuuta 2007, 16:37
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package flowchart.nodes;

import java.awt.*;
import java.util.*;
import java.util.Collections.*;

/**
 *
 * @author jl
 */
public class CompositeGraphicsObject extends AbstractGraphicsObject {
    
    private ArrayList<GraphicsObject> objects;
    private int grid;
  // public int zindex;

    //method for snap to grid
    private void snap(GraphicsObject obj){
        Rectangle objbounds = obj.getBounds();
        double x = objbounds.getX();
        double y = objbounds.getY();
        double width = objbounds.getWidth();
        double height = objbounds.getHeight();
        if (x % grid < 8) {
            x = x - x % grid;
        } else if (x % grid > 7) {
            x = x + (grid - x % grid);
        }
        if (y % grid < 8) {
            y = y - y % grid;
        } else if (y % grid > 7) {
            y = y + (grid - y % grid);
        }

        if (width % grid < 8) {
            width = width - width % grid;
        } else if (width % grid > 7) {
            width = width + (grid - width % grid);
        }
        if (height % grid < 8) {
            height = height - height % grid;
        } else if (height % grid > 7) {
            height = height + (grid - height % grid);
        }
        objbounds.setRect(x, y, width, height);
        obj.setBounds(objbounds);

    }
    
    /** Creates a new instance of CompositeGraphicsObject */
    public CompositeGraphicsObject() {
        objects = new ArrayList();
        grid=0;
    }
    
    public void draw(Graphics2D g) {
        //draw child objects according z-index
        Iterator<GraphicsObject> i = objects.iterator();
        while (i.hasNext()) {
            GraphicsObject obj = i.next();

            //do the snap if grid >0
            if(grid>0)
               snap(obj);

            //draw objects
            obj.draw(g);
        }
    }


    public void setGrid(int offset){
        grid = offset;
    }

    
    @Override
    public void addObject(GraphicsObject obj) {
        //
        objects.add(obj);
         Collections.sort(objects);
    }
    
    @Override
    public void removeObject(GraphicsObject obj) {
        objects.remove(obj);
    }
    
    @Override
    public int getCount() {
        return objects.size();
    }
    
    @Override
    public GraphicsObject getObject(int index) {
        return objects.get(index);
    }


     @Override
    public Rectangle getBounds() {

        int ly=0,lx=0;
        Rectangle cr=null;

        Iterator<GraphicsObject> i = objects.iterator();
        while (i.hasNext()) {
            //Collections.sort(objects);
            GraphicsObject obj = i.next();
            Rectangle t=obj.getBounds();

            if (cr==null){
                cr=t;
            }

            else
            {
                //vasen ylakulma
               if (t.x < cr.x)
                cr.x=t.x;
               if (t.y < cr.y)
                cr.y=t.y;

                //oikea alakulma
                if ((t.x+t.width) > lx)
                    lx=t.x+t.width;
                if ((t.y+t.height)>ly )
                    ly=t.y+t.height;
            }
        }

        int w = lx - cr.x;
        int h = ly - cr.y;

        return new Rectangle(cr.x,cr.y,w,h);
    }


    public void move(int x, int y) {
        Rectangle n = getBounds();
        Point alku = new Point(n.x,n.y);
        n.x=x;
        n.y=y; //ofset missing
        Point loppu = new Point(n.x,n.y);

        Iterator<GraphicsObject> i = objects.iterator();
        while (i.hasNext()) {
            GraphicsObject h = i.next();
            Rectangle hr=h.getBounds();
            //h.move(hr.x - (alku.x - loppu.x),hr.y-(alku.y - loppu.y));
            Rectangle rect = new Rectangle(hr.x - (alku.x - loppu.x),hr.y-(alku.y - loppu.y),this.getBounds().width,this.getBounds().height);
               h.setBounds(rect);
        }
        }

    
}
